package es.chaveweb.poZit.server.domain;

import javax.jdo.PersistenceManager;
import javax.jdo.annotations.Column;
import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.jdo.annotations.Version;
import javax.jdo.annotations.VersionStrategy;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
@Version(column = "version", strategy = VersionStrategy.VERSION_NUMBER, extensions = { @Extension(vendorName = "datanucleus", key = "field-name", value = "version") })
public class PoZit {
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long noteId;

	@Column(length = 1000)
	@Size(max = 1000, message = "Máximo 1000 carácteres. Intenta abreviar lo que tengas que decir :)")
	private String noteText;

	@NotNull
	private Integer x;

	@NotNull
	private Integer y;

	protected long version;

	public Long getId() {
		return getNoteId();
	}

	public Long getNoteId() {
		return noteId;
	}

	public void setNoteId(Long noteId) {
		this.noteId = noteId;
	}

	public String getNoteText() {
		return noteText;
	}

	public void setNoteText(String noteText) {
		this.noteText = noteText;
	}

	public Long getVersion() {
		return version;
	}

	public Integer getX() {
		return x;
	}

	public void setX(Integer x) {
		this.x = x;
	}

	public Integer getY() {
		return y;
	}

	public void setY(Integer y) {
		this.y = y;
	}

	public void persist() {
		PersistenceManager em = entityManager();
		try {
			em.makePersistent(this);
		} finally {
			em.close();
		}
	}

	public static PoZit findPoZit(Long poZitId) {
		if (poZitId == null) {
			return null;
		}
		PersistenceManager em = entityManager();
		try {
			return em.getObjectById(PoZit.class, poZitId);
		} finally {
			em.close();
		}
	}

	public static final PersistenceManager entityManager() {
		return PMF.get().getPersistenceManager();
	}
}
